package com.gushenge.core.requests

import android.app.Activity
import android.net.Uri
import android.util.Log
import com.gushenge.core.base.Base
import com.gushenge.core.beans.*
import com.gushenge.core.dao.GlobalConsts
import com.gushenge.core.dao.MMKVConsts
import com.gushenge.core.dao.MMKVConsts.sub
import com.gushenge.core.dao.MMKVConsts.uid
import com.gushenge.core.httpSign
import com.gushenge.core.time
import com.gushenge.core.toast
import com.rxlife.coroutine.RxLifeScope
import rxhttp.awaitString
import rxhttp.toClass
import rxhttp.wrapper.param.RxHttp
import rxhttp.wrapper.param.upload

object AppRequest {
    /*APP配置*/
    fun appConfig(listener:AppConfig.()->Unit,error:()->Unit){
        RxLifeScope().launch {
            val result = RxHttp.get(GlobalConsts.APP_CONFIG)
                    .add("member_id", MMKVConsts.sub)
                    .add("language", MMKVConsts.language)
                    .toClass<Code<AppConfig>>()
                    .await()
            if (result.code==1){
                result.data?.listener()
            }else{
                result.message.toast()
                error()
            }
        }
    }

    fun appConfig1(listener:AppConfig.()->Unit,error:()->Unit){
        RxLifeScope().launch {
            val result = RxHttp.get(GlobalConsts.APP_CONFIG_New)
                .add("member_id", MMKVConsts.sub)
                .add("language", MMKVConsts.language)
                .toClass<Code<AppConfig>>()
                .await()
            if (result.code==1){
                result.data?.listener()
            }else{
                result.message.toast()
                error()
            }
        }
    }
    fun uploadImageOrVideo(context: Activity, uris:Uri, listener: String.() -> Unit){

        RxLifeScope().launch {
            val result = RxHttp.postForm(GlobalConsts.IMAGE_UPLOAD_2021)
                .add("uid", uid)
                .add("t", time)
                .add("sign", httpSign(uid))
                .add("member_id", sub)
                .addPart(context,"image",uris)
                .upload(this){
                    Log.e("TAG1", "${it}uploadImageOrVideo: +${uris}", )
                }
                .awaitString()
            result.listener()
        }
    }

    /**
     * APP更新检查
     * @param versionCode 当前版本号
     *
     **/
    fun updateApp(versionCode: Long, listener: UpdateAppBean.()->Unit,error:String.()->Unit) {
        RxLifeScope().launch {
            val result = RxHttp.get(GlobalConsts.UPDATE_APP)
                    .add("versionCode",versionCode)
                    .add("member_id",MMKVConsts.sub)
                    .add("language", MMKVConsts.language)
                    .toClass<Code<UpdateAppBean>>()
                    .await()
            if (result.code==1){
                result.data?.listener()
            }else{
                result.message.error()
            }
        }
    }


    /**
     * 启动页广告获取
     */
    fun launch(listener: Launch.()->Unit) {
        RxLifeScope().launch {
            val result = RxHttp.get(GlobalConsts.LAUNCH)
                    .add("member_id",MMKVConsts.sub)
                    .add("language", MMKVConsts.language)
                    .toClass<Code<Launch>>()
                    .await()
            if (result.code==1){
                result.data?.listener()
            }
        }
    }

    /*关于我们*/

    fun abount(listener:AboutInfo.()->Unit){
        RxLifeScope().launch {
            val result = RxHttp.get(GlobalConsts.ABOUT_INFO)
                    .add("member_id",MMKVConsts.sub)
                    .add("language", MMKVConsts.language)
                    .toClass<Code<AboutInfo>>()
                    .await()
            if (result.code==1){
                result.data?.listener()
            }else{
                result.message.toast()
            }
        }
    }
    /*获取推广员ID*/

    fun getMemberId(){
        RxLifeScope().launch {
            val result = RxHttp.get(GlobalConsts.GETMEMBERID)
                    .add("language", MMKVConsts.language)
                .add("member_id",MMKVConsts.sub)
                    .toClass<Code<String>>()
                    .await()
            if (result.code==1){
                MMKVConsts.sub = result.data?:"0"
            }else{
                result.message.toast()
            }
        }
    }

    fun uploadImage(context: Activity,uri: Uri,listener: String.() -> Unit){
        RxLifeScope().launch {
            val result = RxHttp.postForm(GlobalConsts.IMAGE_UPLOAD)
                    .add("language", MMKVConsts.language)
                    .add("uid",MMKVConsts.uid)
                    .add("t", time)
                .add("member_id",MMKVConsts.sub)
                    .add("sign", httpSign(MMKVConsts.uid))
                    .addPart(context,"image",uri)
                    .upload(this){

                    }
                    .awaitString()
            result.listener()
        }
    }

    fun getBaseUrl(key:String,listener: () -> Unit){
//        Base.BASEURL = "http://kaifa.pro.94wan.com"
//        Base.BASEURLV3 = "${ Base.BASEURL}/api/app/v3/"
//        MMKVConsts.hasUrl = true
//        listener()
        if (!MMKVConsts.hasUrl || Base.BASEURL.contains("95php")|| Base.BASEURL.contains("94php")){
            RxLifeScope().launch {
                var result = RxHttp.get("http://work.94hwan.com/api/token.php")
                        .add("author",key)
                    .add("member_id",MMKVConsts.sub)
                        .add("language", MMKVConsts.language)
                        .toClass<String>()
                        .await()
                Base.BASEURL = result
                Base.BASEURLV3 = "$result/api/app/v3/"
                MMKVConsts.hasUrl = true
                listener()
            }
        }else{
            listener()
        }
    }

    fun getPhoneCode(listener: ArrayList<PhoneCodeBean>.() -> Unit){
        RxLifeScope().launch {
            val result = RxHttp.get(GlobalConsts.LANGUAGE)
                .add("member_id",MMKVConsts.sub)
                    .toClass<Code<ArrayList<PhoneCodeBean>>>()
                    .await()
            if (result.code==1){
                result.data?.listener()
            }else{
                result.message.toast()
            }
        }
    }
}